﻿using iCodeStudio.Common;
using iCodeStudio.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace iCodeStudio.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.UserName = Session["userName"];
            return View();
        }

        [AllowAnonymous]
        public ActionResult Login()
        {
            if (Session["isLogin"] != null && Convert.ToBoolean(Session["isLogin"]) == false)
            {
                ViewBag.Error = "用户名或密码错误！";
            }
            Session["LoginedUser"] = null;
            return View();
        }

        [HttpPost,AllowAnonymous]
        public ActionResult Login(string username, string password)
        {
            Session["isLogin"] = false;
            string UserAccount = Request["username"];
            string UserPWD = Request["password"];
            IList<iCode_User> userList = ICode_UserServices.GetAlliCode_User();
            userList = userList.Where(obj => obj.iCode_User_Account.Equals(UserAccount) && obj.iCode_User_Pwd.Equals(UserPWD)).ToList();
            if (userList != null && userList.Count > 0)
            {
                Session["isLogin"] = true;
                Session["LoginedUser"] = userList[0].iCode_User_Account;
                Session["userID"] = userList[0].iCode_User_ID;
                Session["userName"] = userList[0].iCode_User_Name;
                return RedirectToAction("Index");
            }
            else
            {
                Session["isLogin"] = false;
                return RedirectToAction("Login");
            }
        }
    }
}